#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>

struct ListNode {
    int val;
    ListNode* next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode* next) : val(x), next(next) {}
};

ListNode* rotateRight(ListNode* head, int k) {
    if (k == 0 || head == nullptr || head->next == nullptr)
        return head;
    int len = 1;
    ListNode* cur = head;
    while (cur->next)
    {
        len++;
        cur = cur->next;
    }

    int add = len - k % len;
    if (add == len)
        return head;
    cur->next = head;
    while (add--)
    {
        cur = cur->next;
    }
    ListNode* res = cur->next;
    cur->next = nullptr;
    return res;
}